let fs = require("fs");
let originRequest = require('request');
let jquery = require('jquery');
let jsdom = require('jsdom');
let cheerio = require('cheerio');
let iconv = require('iconv-lite');


function readhtmlfromlocalfile() {
    let html = fs.readFileSync('weather101190101.shtml').toString('utf8');
// dom = new jsdom.JSDOM(html);
// $ = jquery(dom.window);
    $ = cheerio.load(html, {decodeEntities: false});
    let lis = $("#7d .t.clearfix li");
    for (let i = 0; i < lis.length; i++) {
        let li = lis.eq(i);
        let date = li.find("h1").text();
        let weather = li.find(".wea").text();
        let temperature = li.find(".tem span").text() + "/" + li.find(".tem i").text();
        console.log(date, weather, temperature);
    }
}


var headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
    'Host': 'www.weather.com.cn',
    'Referer': 'http://www.weather.com.cn/weather/101190101.shtml'
};

function request(url, callback) {
    var options = {
        url: url,
        encoding: null,
        //proxy: 'http://xxx.xxx.xxx.xxx:8888',
        headers: headers
    };
    originRequest(options, callback)
}

request('http://www.weather.com.cn/weather/101190101.shtml', function (err, res, body) {
    let html = iconv.decode(body, 'utf-8');
    let $ = cheerio.load(html, {decodeEntities: false});
    let lis = $("#7d .t.clearfix li");
    let updateTime = $("#update_time").val();
    console.log(updateTime);
    for (let i = 0; i < lis.length; i++) {
        let li = lis.eq(i);
        let date = li.find("h1").text();
        let weather = li.find(".wea").text();
        let temperature = li.find(".tem span").text() + "/" + li.find(".tem i").text();
        console.log(date, weather, temperature);
    }
});